package com.scalar.chiptrack.sales.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.sales.dao.SalesDAO;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.User;


/**
 * Implementation of <strong>Action</strong> that gives customers list,
 * search with customer name, create sales order
 *
 * @author Kesava Kumar Achanala
 * @version $Revision: 1.1.1.1 $ $Date: 2006/12/11 23:43:17 $
 */

public final class SalesTaxAction extends ChipTrackAction {

    private static final String ACTION_NAME = "/taxAndTerms.do";  // Added by Basha (08/08/2003)

    // ----------------------------------------------------- Instance Variables


    // --------------------------------------------------------- Public Methods


    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception Exception if business logic throws an exception
     */
    public ActionForward execute(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception {

            // Extract attributes and parameters we will need

            String action = request.getParameter("action");
            String error_code = "";
            //Bhaskar added the two lines given below 9 july 2003
            HttpSession session = request.getSession();
            User user = (User)session.getAttribute (SessionKeys.USER_KEY);
            // Added by Basha (08/08/2003)
            if(user == null)
            {
	            session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
                return (mapping.findForward ("login"));
            }
            // Added by Basha (08/08/2003)

            /*
             Bhaskar added this if else block on 10 july 2003 to faciliate
             the end user to understand that he cannot make any modifications
             in that screen
             */
            if( user.isActionAllowed ( Functionality.SALES_TAX_TERMS_SETUP,
            User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage" , "FALSE" );
                //System.out.println ( " If condition in STA " );
            }
            else
            {
                request.setAttribute ( "showMessage" , "TRUE" );
                //System.out.println ( " Else condition in STA " );
            }

            //Errors
            HashMap errors = new HashMap();
            HashMap info = new HashMap();

            if((action != null) && (action.equals("Save"))) {
                String[] states = request.getParameterValues("state");
                String[] tax = request.getParameterValues("tax");

                String[] oldStates = request.getParameterValues("oldState");
                String[] oldTax = request.getParameterValues("oldTax");
                String[] type = request.getParameterValues("type");


                //String terms = request.getParameter("terms");
                String[] termsId = request.getParameterValues("termsId");
                String[] title = request.getParameterValues("title");
                String[] terms = request.getParameterValues("terms");

                try {
                    // Modified by Prabhanjan on 23rd Jan 2004. Added the if condition
                    if ( ( states != null && states.length > 0 ) && ( tax != null && tax.length > 0 ) )
                    {
                        error_code = SalesDAO.saveSalesTaxes ( null, states, tax, oldStates, oldTax, type );
                    }


                    //SalesDAO.saveTermsAndConditions(terms);
                    // Modified by Prabhanjan on 23rd Jan 2004. Added the if condition
                    String tersAndConditions = "";
                    if ( ( termsId != null && termsId.length > 0 ) && ( title != null && title.length > 0 ) && ( terms != null && terms.length > 0 ) )
                    {
                        SalesDAO.saveTermsAndConditions ( termsId, title, terms );
                        info.put( INFO_TAX_AND_TERMS_SAVED_SUCCESSFULLY, null );
                        tersAndConditions = "saved";
                    }
                    if ( "SUCCESS".equalsIgnoreCase( error_code ) && !"saved".equalsIgnoreCase( tersAndConditions ) )
                    {
                        info.put( INFO_TAX_AND_TERMS_SAVED_SUCCESSFULLY, null );
                    }
                    else if ( "FAIL".equalsIgnoreCase( error_code ) )
                    {
                        errors.put("Could not Change States which are already in use for Sales Orders","");
                    }
                } catch(Exception ex)
                {
                     errors.put(ERROR_INTERNAL_SERVER_ERROR, ERROR_IN_SAVING_TAX_AND_TERMS);
                     errors.put( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
                     session.setAttribute( SessionKeys.ERROR_REPORT_SUBJECT, " Error: Could Not Save Tax and Terms Details ");
                     session.setAttribute( SessionKeys.ERROR_REPORT, ex );
                }
            }

            request.setAttribute("taxList",SalesDAO.loadSalesTaxes());
            request.setAttribute("termsList",SalesDAO.loadTermsAndConditionsAsList());

            request.setAttribute(SessionKeys.INFO_MESSAGES, info);
            request.setAttribute(SessionKeys.ERROR_MESSAGES, errors);
            return (mapping.findForward("success"));
    }
}
